package com.zch.market.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zch.market.entity.Bill;
import com.zch.market.entity.User;
import com.zch.market.service.BillService;

@Controller
@RequestMapping("BillController")
public class BillController{
	@Autowired
	private BillService bs;
	
	@RequestMapping("aaa")
	public void test(){
		System.out.println("999999999999999999999");
	}
	@RequestMapping("getAllBill")
	public String getAllBill(String billName,String payStatus,String pageIndex,
			HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println(pageIndex);
		if(payStatus==null||payStatus==""){
			payStatus = "-1";
		}
		if(pageIndex==null||pageIndex==""){
			pageIndex = "1";
		}
		//转换
		int isPay = Integer.parseInt(payStatus);
		//获得订单列表
		List<Bill> list = bs.getBillByPage(billName, isPay, Integer.parseInt(pageIndex));
		//获取总页码
		int totalPages = bs.getTotalPage(billName, isPay);
		//将参数存入request域
		request.setAttribute("billName", billName);
		request.setAttribute("payStatus", isPay);
		request.setAttribute("i",(Integer.parseInt(pageIndex)-1)*5);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("list", list);
		request.setAttribute("totalCount", bs.getTotalCounts(billName, isPay));
		request.setAttribute("totalPage", totalPages);
		//转发
		return "admin_bill_list";	
	}

	@RequestMapping("addBill")
	public void addBill(Bill bill,HttpServletRequest request, HttpServletResponse response) throws Exception {
		boolean flag = bs.addBill(bill);
		PrintWriter out = response.getWriter();
		if(flag){
			//添加成功
			String a = URLEncoder.encode("添加成功！", "UTF-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllBill';</script>");
		}else{
			//添加失败
			String a = URLEncoder.encode("添加失败！", "UTF-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllBill';</script>");
		}
		
	}

	@RequestMapping("deleteBill")
	public void deleteBill(int id,HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		boolean flag = bs.deleteBill(id);
		PrintWriter out = response.getWriter();
		if(flag){
			//删除成功
			//解决弹框乱码问题
			String a = URLEncoder.encode("删除成功！", "utf-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllBill';</script>");
		}else{
			//删除失败
			//解决弹框乱码问题
			String a = URLEncoder.encode("删除失败！", "utf-8");
			out.println("<script>alert(decodeURIComponent('"+a+"'));window.location.href='getAllBill';</script>");
		}	
		
	}

	@RequestMapping("updateBill")
	public void updateBill(Bill bill,HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		boolean flag = bs.updateBill(bill);
		PrintWriter out = response.getWriter();
		if(flag){
			//更新成功
			String a = URLEncoder.encode("更新成功！", "UTF-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllBill';</script>");
		}else{
			//更新失败
			String a = URLEncoder.encode("更新失败！", "UTF-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllBill';</script>");
		}
	}
	
	@RequestMapping("getBill")
	public String getUserById(int id,HttpServletRequest request){
		Bill bill = bs.getBillById(id);
		request.setAttribute("bill", bill);
		return "updateBill";
	}

}
